package com.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.result.PageDataInfo;
import com.jl.mapper.MenuMapper;
import com.jl.mapper.RoleMapper;
import com.jl.mapper.RoleMenuMapper;
import com.jl.mapper.UserRoleMapper;
import com.jl.model.Menu;
import com.jl.model.Role;
import com.jl.model.RoleMenu;
import com.jl.model.UserRole;
import com.jl.service.IRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-04-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public void selectRoleDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<Role> qw) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectDataGrid(page,qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public boolean deleteRoleByIds(List<Long> ids) {
        //先删除role_menu 中的数据
        QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
        roleMenuQueryWrapper.in("role_id",ids);
        roleMenuMapper.delete(roleMenuQueryWrapper);
        //然后删除user_role中的数据
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.in("role_id",ids);
        userRoleMapper.delete(userRoleQueryWrapper);
        //最后删除自身
        return baseMapper.deleteBatchIds(ids)>0;
    }

    @Override
    public Map<String, Object> selectResourceMapByUserId(Long userId) {
        Map<String, Object> resourceMap = new HashMap<String, Object>();
        Set<String> urlSet = new HashSet<String>();
        Set<String> roles = new HashSet<String>();
        List<Long> roleIdList =new ArrayList<Long>();

        //通过用户id获取所有角色id
        List<Role> roleList=baseMapper.selectRoleListByUserId(userId);
        //如果没有角色则表示没有任何权限
        if(null == roleList ||  roleList.isEmpty()){
            resourceMap.put("urls", urlSet);
            resourceMap.put("roles", roles);
            resourceMap.put("roleIds", roleIdList);
            return resourceMap;
        }
        //根据角色获取角色id

        for(Role role :roleList){
            if(role!=null){
                roleIdList.add(role.getId());
                roles.add(role.getName());
            }
        }
        //保存角色名
        resourceMap.put("roles", roles);
        resourceMap.put("roleIds", roleIdList);
        //根据所有的角色id去查询所有的资源
        List<Menu> resourceList = menuMapper.selectMenuListByRoleIds(roleIdList);
        if(null == resourceList ||  resourceList.isEmpty()){
            resourceMap.put("urls", urlSet);
            return resourceMap;
        }else{
            for (Menu menu : resourceList){
                if(menu!=null && StringUtils.isNotBlank(menu.getUrl())){
                    urlSet.add(menu.getUrl());
                }
            }
            resourceMap.put("urls", urlSet);
        }
        return resourceMap;
    }


    @Override
    public boolean editRoleMenus(Long roleId,List<Long> addMenu, List<Long> deleteMenu) {

        boolean add = true;
        boolean delete =true;

        if(null!= addMenu && !addMenu.isEmpty()){
         //新增加的权限
            add = roleMenuMapper.insertRoleMenuList(roleId,addMenu) >0;
       }
       if(null!= deleteMenu && !deleteMenu.isEmpty()){
           //减少的权限
           QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<RoleMenu>();
           roleMenuQueryWrapper.eq("role_id",roleId);
           roleMenuQueryWrapper.in("menu_id",deleteMenu);
           delete = roleMenuMapper.delete(roleMenuQueryWrapper) >0;
       }

        return add&&delete;
    }
}
